﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using MB.TheBeerHouse.DAL;

namespace MB.TheBeerHouse.BLL.Articles
{
   public partial class tbh_Comment : BaseArticleEntity
   {
      public string EncodedBody
      {
         get { return Helpers.ConvertToHtml(this.Body); }
      }

      public static IEnumerable<tbh_Comment> GetComments(int articleID)
      {
         using (ArticlesDataContext context = GetContext())
         {
            return context.tbh_Comments.Where(c => c.ArticleID == articleID).OrderBy(c => c.AddedDate).ToList();
         }
      }

      public static IEnumerable<tbh_Comment> GetComments(int startRowIndex, int maximumRows)
      {
         using (ArticlesDataContext context = GetContext())
         {
            context.DeferredLoadingEnabled = false;
            DataLoadOptions loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<tbh_Comment>(c => c.tbh_Article);            
            context.LoadOptions = loadOptions;

            return context.tbh_Comments.OrderByDescending(c => c.AddedDate).Skip(startRowIndex).Take(maximumRows).ToList();
         }
      }

      public static int GetCommentCount()
      {
         using (ArticlesDataContext context = GetContext())
         {
            return context.tbh_Comments.Count(); 
         }               
      }

      public static tbh_Comment GetCommentByID(int commentID)
      {
         using (ArticlesDataContext context = GetContext())
         {
            return context.tbh_Comments.Where(c => c.CommentID == commentID).SingleOrDefault();
         }         
      }

      public static void InsertComment(tbh_Comment comment)
      {
         comment.AddedByIP = CurrentUserIP;
         comment.AddedDate = DateTime.Now;

         using (ArticlesDataContext context = GetContext())
         {
            context.tbh_Comments.InsertOnSubmit(comment);
            context.SubmitChanges();
         }
      }

      public static void UpdateComment(tbh_Comment changedComment)
      {
         using (ArticlesDataContext context = GetContext())
         {
            context.tbh_Comments.Attach(changedComment, true);
            context.SubmitChanges();
         }
      }

      public static void DeleteComment(tbh_Comment comment)
      {
         using (ArticlesDataContext context = GetContext())
         {
            context.tbh_Comments.Attach(comment);
            context.tbh_Comments.DeleteOnSubmit(comment);
            context.SubmitChanges();            
         }
         new RecordDeletedEvent("comment", comment.CommentID, null).Raise();
      }

      // by replacing the DataList in ShowArticle.aspx with a Listview,
      // we no longer need the method below

      //public static void DeleteComment(int commentID)
      //{
      //   using (ArticlesDataContext context = GetContext())
      //   {
      //      tbh_Comment comment = context.tbh_Comments.Where(c => c.CommentID == commentID).SingleOrDefault();
      //      context.tbh_Comments.DeleteOnSubmit(comment);
      //      context.SubmitChanges();      
      //   }
      //   new RecordDeletedEvent("comment", commentID, null).Raise();
      //}
   } 
}